public class 最长公共子序列 {
    public int longestCommonSubsequence(String text1, String text2) {
        char[] t1 = text1.toCharArray();
        char[] t2 = text2.toCharArray();
        int n = t1.length;
        int m = t2.length;
        int[][] dp = new int[n + 1][m + 1];
        // 前 i 个 text1 里面和前 j 个里面最多有多少个
        for (int i = 1;i <= n;i++) {
            for (int j = 1;j <= m;j++) {
                if (t1[i - 1] == t2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i][j - 1],dp[i - 1][j]);
                }
            }
        }
        return dp[n][m];
    }
}
